home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / seritest.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  4KB  |  170 lines

  1. /* serial test program v1.0 */
  2.  
  3. #define SERPORTEVENTS
  4.  
  5. #include        "exec/types.h"
  6. #include        "exec/nodes.h"
  7. #include        "exec/lists.h"
  8. #include        "exec/ports.h"
  9. #include        "exec/libraries.h"
  10. #include        "exec/devices.h"
  11. #include        "exec/io.h"
  12. #include        "devices/serial.h"
  13.  
  14. struct IOExtSer IORser;
  15. struct MsgPort *port;
  16. char   buffer[20];
  17. extern struct MsgPort *CreatePort();
  18.  
  19. main()
  20. {
  21.     int     error;
  22.     int     actual;
  23.     unsigned char iof;
  24.     unsigned long cc;
  25.     unsigned long rbl;
  26.     unsigned long brk;
  27.     unsigned long baud;
  28.     unsigned char rwl;
  29.     unsigned char wwl;
  30.     unsigned char sf;
  31.     unsigned long t0;
  32.     unsigned long t1;
  33.  
  34. open:
  35.  /* OPEN the serial.device */
  36.     if ((error = OpenDevice (SERIALNAME, 0, &IORser, 0)) != 0) {
  37.         printf ("bad news %ld on Open \n", error);
  38.         exit (error);
  39.     }
  40.  
  41.  /* SET UP the message port in the I/O request */
  42.     port = CreatePort (SERIALNAME,0);
  43.     IORser.IOSer.io_Message.mn_ReplyPort = port;
  44.  
  45. /*    SET PARAMS for the serial.device */
  46.     iof = 0x00;
  47.     cc  = 0x00;
  48.     rbl = 4096;
  49.     rwl = 0x08;
  50.     wwl = 0x08;
  51.     brk = 250000;
  52.     baud= 9600;
  53.     sf  = 0x00;
  54.     t0  = 0x51040303;
  55.     t1  = 0x03030303;
  56.  
  57.   if ((error = setparams (iof,cc,rbl,rwl,wwl,brk,baud,sf,t0,t1)) != 0) {
  58.         printf ("bad news %ld on setup \n", error);  
  59.         exit (error);    /* redundant err msg, but who's counting ?? */
  60.     } 
  61.  
  62.     writeSer ("\n\015opened ok\n\015", -1);
  63.     writeSer ("test of -1 length\n\015", -1);
  64.     sendWaitWrite ("this is a sendio test\n\015", 23);
  65.     sendWaitWrite ("this is another sendio test\n\015", -1);
  66.     writeSer ("\n\015type 16 characters to send to amiga\n\015", -1);
  67.     actual = readSer (buffer,16);
  68.     writeSer ("\n\015Buffer was :\n\015", 16); 
  69.     writeSer (buffer, actual); 
  70.     writeSer ("\n\015end\n\015", -1); 
  71.     writeSer ("54321", 5); 
  72.  
  73.  /* CLOSE the serial.device */
  74.     CloseDevice (&IORser);
  75.     DeletePort (port);
  76.     exit (0);
  77. }
  78.  
  79.  /* SERIAL I/O functions */
  80.  
  81. setparams(is,ctl_char,rbuf_len,rlen,wlen,brk,baud,sf,ta0,ta1)
  82.  
  83.     unsigned char is;
  84.     unsigned long ctl_char;
  85.     unsigned long rbuf_len;
  86.     unsigned char rlen;
  87.     unsigned char wlen;
  88.     unsigned long brk;
  89.     unsigned long baud;
  90.     unsigned char sf;
  91.     unsigned long ta0;
  92.     unsigned long ta1;
  93.  
  94. {
  95.     int error;
  96.  
  97.     IORser.IOSer.io_Flags   = is;
  98.     IORser.io_CtlChar       = ctl_char;
  99.     IORser.io_ReadLen       = rlen;
  100.     IORser.io_BrkTime       = brk;
  101.     IORser.io_Baud          = baud;
  102.     IORser.io_WriteLen      = wlen;
  103.     IORser.io_StopBits      = 0x01;
  104.     IORser.io_RBufLen       = rbuf_len;
  105.     IORser.io_SerFlags      = sf;
  106.     IORser.IOSer.io_Command = SDCMD_SETPARAMS;
  107.     IORser.io_TermArray.TermArray0 = ta0;
  108.     IORser.io_TermArray.TermArray1 = ta1;
  109.  
  110.     if ((error = DoIO (&IORser)) != 0) {
  111.         printf ("serial.device setparams error %ld \n", error);
  112.         exit (1);
  113.     }
  114.     return (error);
  115. }
  116.  
  117. readSer(data,length)
  118.     char *data;
  119.     ULONG length;
  120. {
  121.     int error;
  122.  
  123.     IORser.IOSer.io_Data = data;
  124.     IORser.IOSer.io_Length = length;
  125.     IORser.IOSer.io_Command = CMD_READ;
  126.  
  127.     if ((error = DoIO (&IORser)) != 0) {
  128.         printf ("serial.device read error %ld \n", error);
  129.         exit (1);
  130.     }
  131.     return (IORser.IOSer.io_Actual);
  132. }
  133.  
  134.  
  135. writeSer(data,length)
  136.     char *data;
  137.     int length;
  138. {
  139.     int     error;
  140.  
  141.     IORser.IOSer.io_Data = data;
  142.     IORser.IOSer.io_Length = length;
  143.     IORser.IOSer.io_Command = CMD_WRITE;
  144.  
  145.     if ((error = DoIO (&IORser)) != 0) {
  146.         printf ("serial.device write error %ld \n", error);
  147.         exit (1);
  148.     }
  149.     return (error);
  150. }
  151.  
  152. sendWaitWrite(data,length)
  153.     char *data;
  154.     int length;
  155. {
  156.     int     error;
  157.  
  158.     IORser.IOSer.io_Data = data;
  159.     IORser.IOSer.io_Length = length;
  160.     IORser.IOSer.io_Command = CMD_WRITE;
  161.  
  162.     SendIO (&IORser);
  163.  
  164.     if ((error = WaitIO (&IORser)) != 0) {
  165.         printf ("serial.device waitio error %ld \n", error);
  166.         exit (1);
  167.     }
  168.     return (IORser.IOSer.io_Actual);
  169. }
  170.